<!DOCTYPE html>
<html>

<head>
    <title>编辑游记</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/loginIntercept.js"></script>
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/editTravels.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css" />
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/plugins/form/jquery.form.js"></script>
    <script src="/js/My97DatePicker/WdatePicker.js"></script>
    <script src="/js/common.js"></script>
    <script src="/js/checkLogin.js"></script>
    <script src="/js/plugins/eleditor/Eleditor.min.js"></script>
    <script src="/js/plugins/eleditor/webuploader.min.js"></script>
    <script>
        $(function () {


            //设置表单地址(默认新增)
            $("#detailForm").attr('action',baseUrl+"/travels");

            //获取所有的地区数据
            $.get(baseUrl+"/regions",function (data) {
                var temp = "";
                $.each(data,function (index, ele) {
                    temp += ' <option value="'+ele.id+'">'+ele.name+'</option>';
                })
                $("#regionSelect").html(temp);
                //保证地区的option已经拼接好了再进行回显
                var params = getParams();//获取游记id
                if(params.id){//代表是做更新操作
                    //设置表单地址(更新)
                    $("#detailForm").attr('action',baseUrl+"/travels/"+params.id);
                    //设置请求方式
                    $("input[name=_method]").val("put");
                    //根据游记id获取游记对象
                    $.get(baseUrl+"/travels/"+params.id,function (data) {
                        $(".travels").renderValues(data);
                    })
                }
            })


            $("#changeBtn").click(function () {
                $("#fileInput").click();
            })

            $("#fileInput").change(function () {
                if($(this).val()){
                    $("#fileForm").ajaxSubmit( {
                            url: baseUrl + '/images',
                            success: function (data) {
                                if (data.status == 1) {
                                   $("#coverUrl").attr('src',data.url);
                                }
                            }
                    })
                }
            })
            //富文本编辑器
            var Edr = new Eleditor({
                el: '#contentEditor',//要使用编辑器的位置
                upload:{
                    server: baseUrl+'/images', //文件上传的接口路径
                }
            });


            //把表单变成ajax表单
            $("#detailForm").ajaxForm({
                beforeSubmit:function(arr){//提交表单之前会执行的函数,arr是表单提交时所提交的数据
                    //beforeSubmit用途一般就下面这两种
                    //1.自定义逻辑验证表单(控制表单是否提交) return false/true
                    //2.在提交表单前添加额外的参数
                    arr.push({name: "title", value: $("#title").val()})
                    arr.push({name: "coverUrl", value: $("#coverUrl").attr("src")})
                    arr.push({name: "travelContent.content", value: Edr.getContent() })
                    arr.push({name: "state", value: state})
                },
                success:function (data) {
                    window.location.href = "/mine/travels.html";
                }
            })

            var state;//状态

            //保存
            $("#save").click(function () {
                state = 0;//草稿
                $("#detailForm").submit();
            })

            //发布
            $("#release").click(function () {
                state = 1;//待审核
                $("#detailForm").submit();
            })
        })

    </script>
</head>

<body>
<form id="fileForm" enctype="multipart/form-data" method="post">
    <input type="file" name="file" id="fileInput" style="display: none">
</form>


<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:window.history.go(-1);">
                <span><i class="fa fa-times"></i></span>
            </a>
        </div>
        <div class="col-5"></div>
        <div class="col" style="text-align: right;">
            <i class="fa fa-floppy-o fg-fw" id="save"><span>保存</span></i>
            <i class="fa fa-check" id="release"><span>发布</span></i>
        </div>
    </div>
</div>

<div class="travels form-group">
    <div class="cover">
        <img id="coverUrl" width="100%" height="200px" render-src="coverUrl" />
       
            <button type="button" class="btn" id="changeBtn">
                <i class="fa fa-pencil-square-o"></i>
                <small>更换封面</small>
            </button>
       

    </div>
    <div class="title">
        <input class="form-control" id="title"  placeholder="请输入标题" render-value="title"/>
    </div>
    <h6 href="#">点击文章内容即可编辑</h6>
    <div id="contentEditor" render-html="travelContent.content">

    </div>
    <hr/>
    <div class="container">
        <form id="detailForm" method="post" >
            <input type="hidden" name="_method" />
            <label>出行时间:</label>
            <input type="text" name="travelTime"  class="form-control" onclick="WdatePicker()" render-value="travelTime">
            <label>出行天数:</label>
            <input type="number" name="days" class="form-control" render-value="days">
            <label>参与人物:</label>
            <select class="c-select form-control" name="person" render-value="person">
                <option value="1" selected>一个人的旅行</option>
                <option value="2">和父母</option>
                <option value="3">和朋友</option>
                <option value="4">和同事</option>
                <option value="5">和爱人</option>
                <option value="6">和其他</option>
            </select>
            <label>人均费用:</label>
            <input type="text" name="perExpends" class="form-control" render-value="perExpends">

            <label>旅游地点:</label>
            <select class="form-control" id="regionSelect" render-value="place.id" name="place.id" >
            </select>

            <div class="form-check">
                <label class="form-check-label"> <!-- render-attr设置自定义属性-->
                    <input class="form-check-input" type="checkbox"  name="isPublic"
                           render-attr='checked=isPublic'
                          >是否公开
                </label>
            </div>
        </form>
    </div>

    <hr/>

</div>


</body>

</html>